package com.rxyb.hongyu.utils;

import com.alibaba.fastjson.JSONObject;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.po.ThirdHongyuCfg;
import com.rxyb.hongyu.gateway.dto.HongYuReq;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.enums.RspCodeForHongyu;
import com.rxyb.toolize.RxybAlarmUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 记得要字典
 *
 * @coder duyh,@auth duyh
 * @date Nov 29, 2018
 */
@Slf4j
@Service
public class HongYuClient {

    /**
     * 解密报文
     *
     * @param req
     * @return
     */
    public String getReqDoc(ThirdHongyuCfg cfg, HongYuReq req) {
//        if (HongYuConstants.teskKey.equals(req.getJkxlh())) {
//            log.debug("小杜测试，直接跳过验签。。。。。");
//            return req.getReqDoc();
//        }
        String keyChain = cfg.getKeyChain();
        HongyuSignUtils utils = new HongyuSignUtils(keyChain);
        String reqDoc = utils.decrypt(req.getReqDoc());
        if (null == reqDoc) {
            throw new RuntimeException();
        }
        return reqDoc;
    }

    /**
     * 请求鸿宇
     */
    public HongYuRsp post(ThirdHongyuCfg cfg, String funcNo, String data) {
        String result = callExtAccess(cfg.getPostUrl(), HongYuConstants.ACCESS_NAME, cfg.getKeyChain(), HongYuConstants.FUNC_NO, cfg.getSerialNum(), funcNo, data);
        HongYuRsp rsp = null;
        if (null == result) {
            rsp = new HongYuRsp();
            rsp.setRetcode(RspCodeForHongyu.FAILED.getCode());
            rsp.setRetmessage("请求安检异常");
            return rsp;
        }
        try {
            rsp = JSONObject.parseObject(result, HongYuRsp.class);
        } catch (Exception e) {
            String describe = Thread.currentThread().getStackTrace()[1].getClassName() + ":" + Thread.currentThread().getStackTrace()[1].getLineNumber();
            RxybAlarmUtils.warn(null, describe, "鸿宇返回报文转换异常:" + e.getMessage());
            rsp = new HongYuRsp();
            rsp.setRetcode(RspCodeForHongyu.FAILED.getCode());
            rsp.setRetmessage("解析安检报文异常");
        }
        return rsp;
    }

    /**
     * @param endPoint   接口访问地址 例：http://219.139.44.45:12580/jcjkCfgjService/services/
     *                   cfgjService?wsdl
     * @param accessName 调用接口方法名 例：extAccessObj
     * @param xtlb       系统类别 例：20
     * @param jkxlh      接口序列号 例：a00000554d3da3
     * @param jkid       接口ID 例：20A01
     * @param reqDoc     请求参数
     */
    private String callExtAccess(String endPoint, String accessName, String keyChain, String xtlb, String jkxlh, String jkid, String reqDoc) {
        String result = null;
        try {
            log.debug("请求序列号：" + jkxlh);
            log.debug("加密前：" + reqDoc);
            HongyuSignUtils aesUtil = new HongyuSignUtils(keyChain);
            reqDoc = aesUtil.encrypt(reqDoc);
            log.debug("加密后：" + reqDoc);
            org.apache.axis.client.Service service = new org.apache.axis.client.Service();
            org.apache.axis.client.Call call = (org.apache.axis.client.Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endPoint));
            call.setOperationName(accessName);
            try {
                result = (String) call.invoke(new Object[]{xtlb, jkxlh, jkid, reqDoc});
            } catch (Exception e) {
            }
            log.debug("解密前：" + result);
            if (null != result) {
                result = aesUtil.decrypt(result);
                log.debug("解密后：" + result);
            } else {
                log.debug("返回报文为空，无需解密=========================");
            }
        } catch (Exception ex) {
            String transNo = UUID.randomUUID().toString();
            String describe = Thread.currentThread().getStackTrace()[1].getClassName() + ":" + Thread.currentThread().getStackTrace()[1].getLineNumber();
            RxybAlarmUtils.warn(null, describe, transNo + "访问鸿宇异常" + ex.getMessage());
            log.error(transNo + "访问鸿宇异常：", ex);
        }
        return result;
    }

}
